package ng.graphics;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import ng.lib.Vector2D;
import ng.map.Map;
import ng.map.MapObject;

public class Camera {
	private static final int SAFE_DISTANCE = 100;
	private Map map;
	private Rectangle rect;
	private Vector2D speed = new Vector2D();
	private MapObject haunted;
	private Vector2D position;
	private double zoom = 1;

	public Camera(Map m, Rectangle r, Vector2D p) {
		this.map = m;
		this.rect = r;
		this.position = p;
	}

	public Camera(Map m, Rectangle r) {
		this.map = m;
		this.rect = r;
		this.position = new Vector2D();
	}

	public void setZoom(double z) {
		this.zoom = z;
	}

	public void draw(Graphics2D g2) {

		g2.setColor(Color.RED);

		//g2.clipRect(rect.x, rect.y, rect.width, rect.height);
		g2.drawRect(rect.x, rect.y, rect.width, rect.height);
		
		g2.translate(-position.x + rect.x, -position.y + rect.y);
		
		if(zoom!=1)
			g2.scale(zoom, zoom);

		
	
		this.map.draw(g2, new Rectangle((int) position.x - SAFE_DISTANCE,
				(int) position.y - SAFE_DISTANCE,
				(int) (rect.width * (1.0 / zoom)) + SAFE_DISTANCE,
				(int) (rect.height * (1.0 / zoom)) + SAFE_DISTANCE));
		
		if(zoom!=1)
			g2.scale(1.0 / zoom, 1.0 / zoom);

		g2.translate(position.x - rect.x, position.y - rect.y);
		
		g2.setClip(null);

	}

	public Vector2D getSpeed() {
		return this.speed;
	}

	public void tick() {
		this.position.x += this.speed.x;
		this.position.y += this.speed.y;

		if (this.haunted != null) {
			System.out.println("X:" + this.position.x);
			this.position.x = (int) (haunted.getPosition().x - this.rect.width / 2);
			this.position.y = (int) (haunted.getPosition().y - this.rect.height / 2);
		}

	}

	public void setHaunted(MapObject o) {
		this.haunted = o;
	}

}
